package br.unifesp.lma.chamada.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import br.unifesp.lma.chamada.dao.ProfessorDAO;
import br.unifesp.lma.chamada.model.entity.Professor;

@RequestMapping(value = "/admin")
@Controller
public class ProfessorController {

	private static Logger	log	= Logger.getLogger(ProfessorController.class);

	@Autowired
	private ProfessorDAO	professorDAO;

	@RequestMapping(value = "remover-professor", method = RequestMethod.GET)
	public String removerProfessor(Model uiModel, Long id) {

		Professor professor = professorDAO.get(id);
		professorDAO.delete(professor);

		return "redirect:/admin/listar-professores";
	}

	@RequestMapping(value = "editar-professor", method = RequestMethod.GET)
	public String editarProfessor(Model uiModel, Long id) {

		Professor professor = professorDAO.get(id);

		uiModel.addAttribute("active", "professores");
		uiModel.addAttribute("professor", professor);
		return "editar-professor";
	}

	@RequestMapping(value = "adicionar-professor", method = RequestMethod.POST)
	public String salvarProfessor(Model uiModel, Professor professor) {

		professorDAO.put(professor);

		return "redirect:/admin/listar-professores";
	}

	@RequestMapping(value = "adicionar-professor", method = RequestMethod.GET)
	public String adicionarProfessor(Model uiModel) {

		uiModel.addAttribute("active", "professores");
		uiModel.addAttribute("professor", new Professor());
		return "form-professor";
	}

	@RequestMapping(value = "listar-professores", method = RequestMethod.GET)
	public String listarProfessores(Model uiModel) {
		log.debug("Listar professores");
		uiModel.addAttribute("professores", professorDAO.getAll());
		uiModel.addAttribute("active", "professores");
		return "listar-professores";
	}

}
